package com.csnf.dao;

import com.csnf.entity.User;
import com.csnf.util.JdbcUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

    public List<User> getAll() throws SQLException {
        /*Connection conn = DriverManager.getConnection("jdbc:mysql:///tmalldemodb", "root", "root123");
        PreparedStatement ps = conn.prepareStatement("select * from user");*/
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn,"select user_id,user_name,user_nickname from user");
        ResultSet rs = ps.executeQuery();
        List<User> userList = new ArrayList<>();
        while(rs.next()){
            /*User user =new User() ;
            user.setUser_id(rs.getInt("user_id"));
            user.setUser_name(rs.getString("user_name"));
            user.setUser_nickname(rs.getString("user_nickname"));
            userList.add(user);*/
            User user = null;
            try {
                user = JdbcUtil.invokeObject(rs,User.class);
            }catch (Exception e){
                throw new SQLException(e);
            }
            userList.add(user);
        }
        JdbcUtil.close(rs,ps,conn);
        return userList;
    }

    public User getUserById(Integer user_id) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn,"select user_id,user_name,user_nickname from user where user_id = ?",user_id);

        ResultSet rs = ps.executeQuery();
        List<User> userList = new ArrayList<>();
        User user = null;
        if(rs.next()){
            try {
                user = JdbcUtil.invokeObject(rs,User.class);
            }catch (Exception e){
                throw new SQLException(e);
            }
        }
        JdbcUtil.close(rs,ps,conn);
        return user;
    }

    public List<User> getUserByNickName(String nickName) throws SQLException {
        /*Connection conn = DriverManager.getConnection("jdbc:mysql:///tmalldemodb", "root", "root123");
        PreparedStatement ps = conn.prepareStatement("select * from user where user_nickname like ?");
        ps.setString(1, "%"+nickName+"%");*/
        Connection conn = JdbcUtil.getConnection();
        String sql = "select user_id,user_name,user_nickname from user where user_nickname like concat('%',?,'%')";
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn,sql,nickName);
        ResultSet rs = ps.executeQuery();
        List<User> userList = new ArrayList<>();
        while(rs.next()){
            /*User user =new User() ;
            user.setUser_id(rs.getInt("user_id"));
            user.setUser_name(rs.getString("user_name"));
            user.setUser_nickname(rs.getString("user_nickname"));
            userList.add(user);*/
            User user = null;
            try {
                user = JdbcUtil.invokeObject(rs,User.class);
            }catch (Exception e){
                throw new SQLException(e);
            }
            userList.add(user);
        }
        JdbcUtil.close(rs,ps,conn);
        return userList;
    }

    /*
    * @paran user
    * @return
    *
     */
    public boolean updateUser(User user) throws SQLException{
        Connection conn = JdbcUtil.getConnection();
        String sql = "update user set user_name=?,user_nickname=? where user_id=?";
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn,sql,
                user.getUser_name(),
                user.getUser_nickname(),
                user.getUser_id());
        int rows = ps.executeUpdate();
        JdbcUtil.close(ps,conn);
        return rows>0;
    }

    public boolean deleteUser(int i) throws SQLException{
        Connection conn = JdbcUtil.getConnection();
        String sql = "delete from user where user_id=?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, i);
            int rows = ps.executeUpdate();
            JdbcUtil.close(ps, conn);
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                if (ps!= null) {
                    ps.close();
                }
                if (conn!= null) {
                    conn.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return false;
        }
    }
}
